package com.yu.waterstation.system.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.waterstation.common.entity.app.TaskLog;
import com.yu.waterstation.system.modules.app.mapper.TaskLogMapper;
import com.yu.waterstation.system.modules.app.service.TaskLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class TaskLogServiceImpl implements TaskLogService {

    @Resource
    private TaskLogMapper taskLogMapper;

    /**
     * 分页条件查询
     * 联合索引 collegeId_timestamp_dorm_room
     *
     * @param startTime  起始时间
     * @param dorm       楼舍
     * @param room       房间号
     * @param workerName 送水员姓名
     * @param pageNo     第几页
     * @param pageSize   页面大小
     * @return
     */
    @Override
    public Page<TaskLog> searchTaskLogs(long startTime, String dorm, String room, String workerName,
                                        int pageNo, int pageSize)   {
        QueryWrapper<TaskLog> wrapper =new QueryWrapper<TaskLog>();
        // 走联合索引查询 顺序不能变
        wrapper.ge("timestamp", new Date(startTime));
        if (!StringUtils.isEmpty(dorm)) {
            wrapper.eq("dorm", dorm);
            if (!StringUtils.isEmpty(room))
                wrapper.eq("room", room);
        }
        if (!StringUtils.isEmpty(workerName))
            wrapper.like("worker_name", workerName);
        wrapper.orderByDesc("timestamp");

        Page<TaskLog> page = new Page<>(pageNo, pageSize);
        return taskLogMapper.selectPage(page, wrapper);
    }
}
